package cn.iocoder.yudao.module.handguard.service.userbasetable2;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.handguard.controller.app.userbasetable2.vo.*;
import cn.iocoder.yudao.module.handguard.dal.dataobject.userbasetable2.UserBaseTable2DO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.handguard.convert.userbasetable2.UserBaseTable2Convert;
import cn.iocoder.yudao.module.handguard.dal.mysql.userbasetable2.UserBaseTable2Mapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.handguard.enums.ErrorCodeConstants.*;

/**
 * 保单贷 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class UserBaseTable2ServiceImpl implements UserBaseTable2Service {

    @Resource
    private UserBaseTable2Mapper userBaseTable2Mapper;

    @Override
    public Long createUserBaseTable2(UserBaseTable2CreateReqVO createReqVO) {
        // 插入
        UserBaseTable2DO userBaseTable2 = UserBaseTable2Convert.INSTANCE.convert(createReqVO);
        userBaseTable2Mapper.insert(userBaseTable2);
        // 返回
        return userBaseTable2.getId();
    }

    @Override
    public void updateUserBaseTable2(UserBaseTable2UpdateReqVO updateReqVO) {
        // 校验存在
        validateUserBaseTable2Exists(updateReqVO.getId());
        // 更新
        UserBaseTable2DO updateObj = UserBaseTable2Convert.INSTANCE.convert(updateReqVO);
        userBaseTable2Mapper.updateById(updateObj);
    }

    @Override
    public void deleteUserBaseTable2(Long id) {
        // 校验存在
        validateUserBaseTable2Exists(id);
        // 删除
        userBaseTable2Mapper.deleteById(id);
    }

    private void validateUserBaseTable2Exists(Long id) {
        if (userBaseTable2Mapper.selectById(id) == null) {
            throw exception(USER_BASE_TABLE2_NOT_EXISTS);
        }
    }

    @Override
    public UserBaseTable2DO getUserBaseTable2(Long id) {
        LambdaQueryWrapper<UserBaseTable2DO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserBaseTable2DO::getApplicationId,id);
        return userBaseTable2Mapper.selectOne(queryWrapper);
    }

    @Override
    public List<UserBaseTable2DO> getUserBaseTable2List(Collection<Long> ids) {
        return userBaseTable2Mapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<UserBaseTable2DO> getUserBaseTable2Page(UserBaseTable2PageReqVO pageReqVO) {
        return userBaseTable2Mapper.selectPage(pageReqVO);
    }

    @Override
    public List<UserBaseTable2DO> getUserBaseTable2List(UserBaseTable2ExportReqVO exportReqVO) {
        return userBaseTable2Mapper.selectList(exportReqVO);
    }

}
